package com.novellotus.iov.carvice.adapter;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.bean.RxCarListBean;
import com.novellotus.iov.carvice.bean.RxEusers;
import com.novellotus.iov.carvice.utils.MyUtils;
import com.novellotus.iov.carvice.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Rich on 2018/1/20.
 *
 */

public class EmployeeListAdapter extends RecyclerView.Adapter<EmployeeListAdapter.EmployeeHolder> {

    private List<RxEusers.RecordsBean> list;
    private ClickEmployee clickEmployee;

    public EmployeeListAdapter(List<RxEusers.RecordsBean> list, ClickEmployee clickEmployee) {
        this.list = list;
        this.clickEmployee = clickEmployee;
        if (list == null) {
            this.list = new ArrayList<>();
        }
    }


    @Override
    public EmployeeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_employee_item, null);
        return new EmployeeHolder(view);
    }

    @Override
    public void onBindViewHolder(EmployeeHolder holder, final int position) {
        if (list.get(position) != null) {
            holder.tv_name.setText(MyUtils.getRealValue(list.get(position).getEUserName()));
            holder.tv_role.setText(MyUtils.getRealValue(list.get(position).getRoleName()));
            holder.tv_email.setText(MyUtils.getRealValue(list.get(position).getEmail()));
            holder.tv_mobile.setText(MyUtils.getRealValue(list.get(position).getMobile()));
            holder.tv_time.setText(TimeUtils.stampTotime(list.get(position).getCreateTime() + ""));
            holder.rlt_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    clickEmployee.clickItem(list.get(position));
                }
            });
            GradientDrawable bgShape = (GradientDrawable) holder.tv_role.getBackground();
            bgShape.setStroke(1, Color.parseColor("#2eb9fc"));
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    /**
     * @param records 刷新数据
     */
    public void update(List<RxEusers.RecordsBean> records) {
        this.list = records;
        notifyDataSetChanged();
    }

    /**
     * @param records 添加下拉时的
     */
    public void addData(List<RxEusers.RecordsBean> records) {
        this.list.addAll(records);
        notifyDataSetChanged();
    }

    class EmployeeHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.tv_role)
        TextView tv_role;
        @BindView(R.id.tv_email)
        TextView tv_email;
        @BindView(R.id.tv_mobile)
        TextView tv_mobile;
        @BindView(R.id.tv_time)
        TextView tv_time;
        @BindView(R.id.rlt_layout)
        RelativeLayout rlt_layout;

        EmployeeHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface ClickEmployee {
        void clickItem(RxEusers.RecordsBean recordsBean);
    }

}
